/**
 * Copyright (c) 2014, Dallas.Cao 曹斌 (adffice@gmail.com).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.adffice.library.dbhelper.table;


import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * Created by dallas on 14-4-13.
 */
public class Property {
    private String mFieldName;
    private String mColumn;
    private String mDefaultVal;
    private Class<?> mDataType;
    private Field mField;

    private Method mGetMethod;
    private Method mSetMethod;

    /**
     * 执行某个实体类的setter方法
     *
     * @param receiver
     * @param value
     */
    public void setValue(Object receiver, Object value) {
        if (mSetMethod != null) {
            try {
                mSetMethod.invoke(receiver, value);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
        } else {
            try {
                mField.setAccessible(true);
                mField.set(receiver, value);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
    }

    public Object getValue(Object obj) {
        if (obj != null && mGetMethod != null) {
            try {
                return mGetMethod.invoke(obj);
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    public String getFieldName() {
        return mFieldName;
    }

    public void setFieldName(String fieldName) {
        this.mFieldName = fieldName;
    }

    public String getColumn() {
        return mColumn;
    }

    public void setColumn(String column) {
        this.mColumn = column;
    }

    public String getDefaultVal() {
        return mDefaultVal;
    }

    public void setDefaultVal(String defaultVal) {
        this.mDefaultVal = defaultVal;
    }

    public Class<?> getDataType() {
        return mDataType;
    }

    public void setDataType(Class<?> dataType) {
        this.mDataType = dataType;
    }

    public Field getmField() {
        return mField;
    }

    public void setField(Field field) {
        this.mField = field;
    }

    public Method getGetMethod() {
        return mGetMethod;
    }

    public void setGetterMethod(Method getMethod) {
        this.mGetMethod = getMethod;
    }

    public Method getSetMethod() {
        return mSetMethod;
    }

    public void setSetterMethod(Method setMethod) {
        this.mSetMethod = setMethod;
    }
}
